from mpi4py import MPI
import numpy as np
comm = MPI.COMM_WORLD
rank = comm.Get_rank()
size = comm.Get_size()

randNum = np.zeros(1)
if rank == 0:
    randNum = np.random.random_sample(1)
    comm.Send(randNum, dest=rank + 1)
    print(f"Process {rank} before receiving has the number {randNum[0]}")
    comm.Recv(randNum, source=size - 1)
    print(f"Process {rank} received the number {randNum[0]}")

if 0 < rank < size - 1:
    randNum = np.random.random_sample(1)
    comm.Send(randNum, dest=rank + 1)
    print(f"Process {rank} before receiving has the number {randNum[0]}")
    comm.Recv(randNum, source=rank - 1)
    print(f"Process {rank} received the number {randNum[0]}")

if rank == size - 1:
    randNum = np.random.random_sample(1)
    comm.Send(randNum, dest=0)
    print(f"Process {rank} before receiving has the number {randNum[0]}")
    comm.Recv(randNum, source=rank - 1)
    print(f"Process {rank} received the number {randNum[0]}")
